package io.zt.dangyuan;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import io.zt.dangyuan.customView.OnRefreshListener;
import io.zt.dangyuan.customView.RefreshListView;
import io.zt.dangyuan.object.ContentListObject;


public class SearchActivity extends BaseActivity implements OnRefreshListener {

    private  TextView  confirmTV;
    private  EditText  inputeET;

    private  int  type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("搜索");
        addCenterView(R.layout.activity_search);
        toolbar.setNavigationIcon(R.drawable.icon_pre);

        listView  =(RefreshListView)findViewById(R.id.activity_search_listview);
        bbsMainAdapter2 = new BBSItemAdapter1(this, listObejects);
        Intent intent = getIntent();

        cid = intent.getStringExtra("cid");
        type = intent.getIntExtra("type",BBSDetailActivity.TYPE_ZHONGCHOU);

        // 初始化
        confirmTV   = (TextView)findViewById(R.id.activity_search_sumbit);
        inputeET   = (EditText)findViewById(R.id.activity_search_context);
        confirmTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!TextUtils.isEmpty(inputeET.getText())) {
                    listObejects.clear();
                    bbsMainAdapter2.notifyDataSetChanged();
                    contentListRequest.setSearchKey(inputeET.getText().toString());
//                    inputeET.setText("");
                } else {
                    contentListRequest.setSearchKey(null);

                    Toast.makeText(SearchActivity.this,"请输入关键字",Toast.LENGTH_SHORT).show();

                    return;
                }
                webRequst(true);
            }
        });


        listView.setAdapter(bbsMainAdapter2);
        listView.setOnRefreshListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(SearchActivity.this, BBSDetailActivity.class);
                ContentListObject.ListEntity object = listObejects.get(position - 1);
                intent.putExtra("contentid", object.getArcId());
                intent.putExtra("type",type);
                intent.putExtra("commotcount", object.getComments());
                startActivity(intent);

            }
        });

//        webRequst();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
        }
        return true;
    }

    @Override
    public void onDownPullRefresh() {

        if (TextUtils.isEmpty(inputeET.getText())) {
            listView.hideHeaderView();
            return;
        }

        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                listObejects.clear();
                contentListRequest.setTapType("1");
                webRequst(false);
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                listView.hideHeaderView();
            }
        }.execute(new Void[]{});

    }

    @Override
    public void onLoadingMore() {
        if (TextUtils.isEmpty(inputeET.getText())) {
            listView.hideFooterView();
            return;
        }
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                contentListRequest.setTapType("2");
                webRequst(false);
//                if (readLoveRequest.isHasMoreData()) {
//                    readLoveRequest.setCurrpage(readLoveRequest.getCurrpage() + 1);
//                    webRequst();
//                } else {
//                    h.sendEmptyMessage(0);
//                }
                return null;
            }
            @Override
            protected void onPostExecute(Void result) {
                // 控制脚布局隐藏
                listView.hideFooterView();
            }
        }.execute(new Void[]{});

    }
}
